﻿CREATE VIEW [Mapping].[vSample_View]
  AS 
  SELECT
    SourceKey.mmlb AS Src_mmlb, SourceKey.zqlb AS Src_degistid,
    [Priority], [Masked], [Enabled],
    [TargetKeyValue],
    CAST([TargetKeyAlternateValue] AS int) AS KeyAlternateValue
  FROM Mapping.vMappingItemView
  JOIN (
    SELECT * 
      FROM (
        SELECT MappingItem, KeyColumn, KeyValue 
          FROM Mapping.SourceKey 
      ) t
      PIVOT (MAX(KeyValue) FOR KeyColumn IN ([mmlb], [zqlb])) p
  ) SourceKey ON SourceKey.MappingItem = vMappingItemView.Id
  WHERE [GroupId] = 1
